package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actsignreward;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.*;

public class ActiveModule_签到 extends PlayerModule {

	// bean ----------------------------------------------

	public static final int acttype = 101;

	private int year; // 年
	private int month; // 月
	private int monthdays; // 这个月有多少天
	private ArrayList<Integer> punchins; // 签到组
	private int loginin; // 登陆值 登陆值 > 签到值 可领取
	private DateTime logininupdatetime; // 登陆值更新时间
	private ArrayList<Integer> paygets; // 支付签到组
	private DateTime paytime; // 支付时间

	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getMonthdays() {
		return monthdays;
	}
	public void setMonthdays(int monthdays) {
		this.monthdays = monthdays;
	}
	public ArrayList<Integer> getPunchins() {
		return punchins;
	}
	public void setPunchins(ArrayList<Integer> punchins) {
		this.punchins = punchins;
	}
	public int getLoginin() {
		return loginin;
	}
	public void setLoginin(int loginin) {
		this.loginin = loginin;
	}
	public DateTime getLogininupdatetime() {
		return logininupdatetime;
	}
	public void setLogininupdatetime(DateTime logininupdatetime) {
		this.logininupdatetime = logininupdatetime;
	}
	public ArrayList<Integer> getPaygets() {
		return paygets;
	}
	public void setPaygets(ArrayList<Integer> paygets) {
		this.paygets = paygets;
	}
	public DateTime getPaytime() {
		return paytime;
	}
	public void setPaytime(DateTime paytime) {
		this.paytime = paytime;
	}

	// fun --------------------------------------------------

	/**
	 * 初始化
	 * 
	 * @return
	 */
	static public ActiveModule_签到 create(String playerCode) {
		ActiveModule_签到 am = new ActiveModule_签到();

		DateTime dt0 = new DateTime(0);
		DateTime nowdt = DateTime.now();
		int year = nowdt.getYear();
		int month = nowdt.getMonthOfYear();
		int monthdays = getDaysOfMonth(nowdt.toDate());
		am.setPlayerCode(playerCode);
		am.setYear(year);
		am.setMonth(month);
		am.setMonthdays(monthdays);
		am.setPunchins(new ArrayList<Integer>());
		am.setLoginin(0);
		am.setLogininupdatetime(dt0);
		am.setPaygets(new ArrayList<Integer>());
		am.setPaytime(dt0);

		am.set();
		return am;
	}

	/**
	 * 获取可以领取得签到组
	 * 
	 * @return
	 */
	public ArrayList<Integer> cangetlist() {
		ArrayList<Integer> list = new ArrayList<Integer>();
		int loginin = getLoginin();
		ArrayList<Integer> pcs = getPunchins();
		for (int i = 1; i <= loginin; i++) {
			list.add(i);
		}
		list.removeAll(pcs);
		return list;
	}

	/**
	 * 能否领取充值奖励签到
	 * 
	 * @return
	 */
	public int candrawpaysignin() {
		DateTime now = DateTime.now();
		DateTime pay = getPaytime();

		int maxpunchin = getLoginin();
		for (int i = 0; i < getPaygets().size(); i++) {
			if (maxpunchin == getPaygets().get(i)) {
				return DrawStatus.hadget.getStatus();
			}
		}

		if (ActiveUtil.isSameDay(pay, now)) {
			return DrawStatus.canget.getStatus();
		}
		return DrawStatus.cannotget.getStatus();
	}

	public ArrayList<Currency> signininfo() {
		ArrayList<Currency> curs = new ArrayList<Currency>();

		int monthdays = getMonthdays();
		Map<String, actsignreward> asrs = ActDataCenter.getAllData(actsignreward.class, getPlayerCode());

		monthdays = monthdays < asrs.size() ? monthdays : asrs.size();

		ArrayList<String> asrkeys = new ArrayList<String>(asrs.keySet());
		Collections.sort(asrkeys);

		for (int i = 0; i < monthdays; i++) {
			String key = asrkeys.get(i);
			actsignreward asr = asrs.get(key);
			ArrayList<Currency> curlist = (ArrayList<Currency>) Currency.create(asr.getRewardtype(), asr.getRewardid(), asr.getRewardnum());
			if (curlist.size() >= 1) {
				Currency cur = curlist.get(0);
				curs.add(cur);
			}
		}

		return curs;
	}

	/**
	 * 领取奖励
	 * 
	 * @return
	 */
	public Currency drawreward(String pcode, int day) {
		Currency cur = new Currency();

		ArrayList<Integer> punchins = getPunchins();

		if (day > getLoginin()) {
			return null;
		}

		Map<String, actsignreward> asrmap = ActDataCenter.getAllData(actsignreward.class, getPlayerCode());
		ArrayList<String> asrkeys = new ArrayList<String>(asrmap.keySet());
		if (day > asrkeys.size()) {
			return null;
		}
		Collections.sort(asrkeys);

		for (int i = 0; i < punchins.size(); i++) {
			int punchin = punchins.get(i);
			if (day == punchin) {
				DateTime now = DateTime.now();
				ArrayList<Integer> pgs = getPaygets();
//				int dayofmonth = now.getDayOfMonth();
				for (int j = 0; j < pgs.size(); j++) {
					if (day == pgs.get(j)) {
						return null;
					}
				}

				DateTime pay = getPaytime();
				if (ActiveUtil.isSameDay(pay, now)) {
					String key = asrkeys.get(day - 1);
					actsignreward asr = asrmap.get(key);
					ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(asr.getRewardtype(), asr.getRewardid(), asr.getRewardnum());
					cur = curs.get(0);
					pgs.add(day);
					setPaygets(pgs);
					this.set();
					return cur;
				}

				return null;
			}
		}

		String key = asrkeys.get(day - 1);
		actsignreward asr = asrmap.get(key);
		ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(asr.getRewardtype(), asr.getRewardid(), asr.getRewardnum());
		cur = curs.get(0);

		getPunchins().add(day);
		this.set();
//		CurrencyHandle ch = new CurrencyHandle();
//		ch.addCurrency(pcode, CCReason.活动_签到, cur);
		return cur;
	}

	/**
	 * 登陆对应处理
	 */
	public void handlelogin() {
		DateTime nowdt = DateTime.now();

		int loginin = getLoginin();
		if (loginin + 1 <= getMonthdays() && ActiveUtil.days(nowdt, getLogininupdatetime()) >= 1) {
			setLoginin(loginin + 1);
			setLogininupdatetime(nowdt);
			this.set();
		}
	}

	public void dealpay() {
		DateTime now = DateTime.now();
		setPaytime(now);
		this.set();
	}

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;

	public static int getDaysOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	private void update() {
		DateTime nowdt = DateTime.now();
		int year = nowdt.getYear();
		int month = nowdt.getMonthOfYear();

		boolean refresh = false;
		if (year > getYear()) {
			refresh = true;
		}
		if (month > getMonth()) {
			refresh = true;
		}

		if (refresh) {
			int nowyear = nowdt.getYear();
			int nowmonth = nowdt.getMonthOfYear();
			int nowmonthdays = getDaysOfMonth(nowdt.toDate());

			setYear(nowyear);
			setMonth(nowmonth);
			setMonthdays(nowmonthdays);
			setPunchins(new ArrayList<Integer>());
			setLoginin(0);
			setLogininupdatetime(new DateTime(0));
			setPaygets(new ArrayList<Integer>());
			setPaytime(new DateTime(0));
			this.set();
		}

		// loginin + 1 <= getMonthdays() &&
		if (ActiveUtil.days(getLogininupdatetime(), nowdt) >= 1) {
			int loginin = getLoginin();
			setLoginin(loginin + 1);
			setLogininupdatetime(nowdt);
			this.set();
		}
	}

	// redis ---------------------------------------------------

	static private String key(String pcode) {
		return String.format("PLAYER:%s:ACTIVE:%S", pcode, acttype);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public ActiveModule_签到 get(String playerCode, Boolean isNewPlayer) {
		try {
			ActiveModule_签到 module = getv(key(playerCode));
			//新用户不需要去查数据库
			if (module == null && !isNewPlayer) {
				module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_签到.class);
				if(module != null){module.set();}//同步到redis
			}

			if (module == null) {
				module = create(playerCode);
			}
			if(module == null){return null;}

			module.update();
			module.check();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
		
		if (cangetlist().size() > 0) {
			new TipHandle().updateTip(getPlayerCode(), TipType.活动_签到奖励, true);
			return;
		}
		if (candrawpaysignin() == DrawStatus.canget.getStatus()) {
			new TipHandle().updateTip(getPlayerCode(), TipType.活动_签到奖励, true);
			return;
		}
		new TipHandle().updateTip(getPlayerCode(), TipType.活动_签到奖励, false);
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_签到 activeModule_签到 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_签到);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
